﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;

namespace Xceed.Silverlight.Data
{
  public class DataItem
  {
    #region Constructors

    public DataItem()
      : this( null, false )
    {
    }

    public DataItem( object data )
      : this( data, true )
    {
    }

    internal DataItem( object data, bool isLoaded )
    {
#if DEBUG
#endif

      m_isLoaded = isLoaded;
      m_data = data;

      if( isLoaded )
      {
        m_hashCode = ( data != null ) ? data.GetHashCode() : 0;
      }
      else
      {
        m_hashCode = base.GetHashCode();
      }
    }

    #endregion

    #region IsLoaded Property

    public bool IsLoaded
    {
      get
      {
        return m_isLoaded;
      }
    }

    private readonly bool m_isLoaded; //false

    #endregion

    #region Data Property

    public object Data
    {
      get
      {
        return m_data;
      }
    }

    private readonly object m_data; //null

    #endregion

    public override int GetHashCode()
    {
      return m_hashCode;
    }

    public override bool Equals( object obj )
    {
      // Two different DataItem instance are consider equals if they are
      // both loaded and contain an equal data object.  A DataItem that is
      // empty or not loaded must not be equal to any other DataItem except
      // itself in order to represent different virtualized items.
      if( obj == this )
        return true;

      if( ( obj == null ) || ( !m_isLoaded ) || ( m_data == null ) )
        return false;

      DataItem dataItem = obj as DataItem;
      if( ( dataItem == null ) || ( !dataItem.m_isLoaded ) || ( dataItem.m_data == null ) )
        return false;

      return m_data.Equals( dataItem.m_data );
    }

#if DEBUG

    public override string ToString()
    {
      string data = ( this.Data == null ) ? "#NULL#" : this.Data.ToString();
      return (this.IsLoaded)
        ? data
        : data + " (not loaded)";
    }
#endif  //DEBUG

    #region Private Fields

    private readonly int m_hashCode;

    #endregion
  }
}
